package io.vector.study.algorithm.sort;

import io.vector.study.algorithm.heap.Heap;
import io.vector.study.algorithm.heap.MinHeap;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * Created by vector on 2017/3/17.
 */
public class HeapSort implements SortStrategy {

    @Override
    public <T extends Comparable<? super T>> void sort(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            return ;
        }

        Heap<T> minHeap = new MinHeap<T>();
        list.forEach(minHeap::add);

        for (int index = 0; index < list.size(); ++index) {
            list.set(index, minHeap.pop());
        }
    }

}
